package com.huazhi.wzsnw.dtu;

import com.huazhi.wzsnw.mapper.*;
import com.huazhi.wzsnw.model.*;
import com.huazhi.wzsnw.timer.AlterUtil;
import com.huazhi.wzsnw.util.redis.RedisCache;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class DtuServerHandle extends ChannelInboundHandlerAdapter {

    private static LinedMapper linedMapper;

    private static LinedStateMapper linedStateMapper;

    private static AlertMapper alertMapper;

    private static AlertValueMapper alertValueMapper;

    private static DtuRecordMapper dtuRecordMapper;

    private static DdlinedMapper ddlinedMapper;

    private static RedisCache redisCache;

    private  final  static  String PREFIX_ID_NUM = "SHORT_ID_NUM:";
    private  final  static  String PREFIX_DKLJ_NUM = "DKLJ_NUM:";
    private  final  static  String PREFIX_NBYC_NUM  = "NBTC_NUM:";
    private  final  static  String PREFIX_HEART_NUM = "HEART_NUM:";
    private  final  static  String PREFIX_HEART_DATA_NUM = "HEART_DATA_NUM:";
    private  final  static  String PREFIX_SEND_DATA_NUM = "SEND_DATA_NUM:";
    private  final  static  String PREFIX_SEN_DATA_TIME ="SEND_DATA_TIME:";
    private  final  static  String PREFIX_SEN_HEART_TIME = "SEND_HEART_TIME:";
    private DateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    AlterUtil alterUtil = new AlterUtil();


    static {
        linedMapper = SpringUtils.getBeen(LinedMapper.class);
        linedStateMapper = SpringUtils.getBeen(LinedStateMapper.class);
        alertValueMapper = SpringUtils.getBeen(AlertValueMapper.class);
        alertMapper = SpringUtils.getBeen(AlertMapper.class);
        dtuRecordMapper = SpringUtils.getBeen(DtuRecordMapper.class);
        ddlinedMapper = SpringUtils.getBeen(DdlinedMapper.class);
        redisCache = SpringUtils.getBeen(RedisCache.class);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String ctxId = ctx.channel().id().asShortText();
        ByteBuf byteBuf = (ByteBuf) msg;
        ByteBuf result = Unpooled.copiedBuffer("wsok!".getBytes());
        // 回复给客户端的数据
        ctx.writeAndFlush(result);
        byte[] data = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(data);
        String xiaoxi = new String(data);
        if (!xiaoxi.contains("DTU")) {
            if (xiaoxi.contains("WZSNWV1") || xiaoxi.contains("DSG4RV1")) {
                String bianhao = xiaoxi.substring(3, 19);
                if (bianhao.startsWith("DSG4RV1") || bianhao.startsWith("WZSNWV1")) {
                    //将最新一次发来的心跳存到redis
                    pcDklj(bianhao);
                    redisCache.setCacheObject(PREFIX_SEN_HEART_TIME+bianhao,dateFormat.format(new Date()));
                    redisCache.setCacheObject(PREFIX_ID_NUM+ctxId,bianhao,5,TimeUnit.HOURS);
                    redisCache.setCacheObject(PREFIX_HEART_NUM+bianhao,bianhao,2, TimeUnit.HOURS);
                } else {
                    System.out.println("编号有问题");
                    System.out.println(xiaoxi);
                }

            } else {
             //先根据通道编号找到num
               String  bianhao = redisCache.getCacheObject(PREFIX_ID_NUM+ctxId);
                if (bianhao != null) {
                    //排查内部异常
                    pcNbjc(bianhao);
                    if (bianhao.contains("WZSNWV1")) {
                        //中海龙设备
                        String dtuData = bytesToHexString(data);
                        String jinzhi = dtuData.substring(0, 119);
                        //先获取存储的数据
                        String oldDtuData = redisCache.getCacheObject(PREFIX_SEND_DATA_NUM+bianhao);
                        redisCache.setCacheObject(PREFIX_SEN_DATA_TIME+bianhao,dateFormat.format(new Date()));
                        if(oldDtuData == null){
                            redisCache.setCacheObject(PREFIX_SEND_DATA_NUM+bianhao,jinzhi);
                            System.out.println("编号为"+bianhao+"的dtu第一次发送数据，存储数据表");
                          //如果是第一次 操作数据库存储
                          getData(bianhao,jinzhi);
                        }else {
                          //如果不是第一次开始比较本次与上次的数据是否相同
                          if(!oldDtuData.equals(jinzhi)){
                            getData(bianhao,jinzhi);
                            redisCache.setCacheObject(PREFIX_SEND_DATA_NUM+bianhao,jinzhi);
                              System.out.println("编号为"+bianhao+"的dtu更新数据");
                          }
                        }
                        System.out.println("编号为"+bianhao+"发送的数据与上次相同，跳过数据表的操作");
                        redisCache.setCacheObject(PREFIX_HEART_DATA_NUM+bianhao,bianhao,2,TimeUnit.HOURS);
                    } else {
                        //东大设备
                        String dtuData = bytesToHexString(data);
                        Map<String, String> allState = getAllState(dtuData);
                        //先获取存储的数据
                        String oldDtuData = redisCache.getCacheObject(PREFIX_SEND_DATA_NUM+bianhao);
                        if(oldDtuData == null){
                            updateDongda(bianhao, allState);
                            redisCache.setCacheObject(PREFIX_SEND_DATA_NUM+bianhao,dtuData);
                            System.out.println("编号为"+bianhao+"的dtu第一次发送数据，存储数据表");
                        }else {
                          if(!oldDtuData.equals(dtuData)){
                          updateDongda(bianhao,allState);
                          redisCache.setCacheObject(PREFIX_HEART_DATA_NUM+bianhao,dtuData);
                              System.out.println("编号为"+bianhao+"的dtu更新数据");
                          }
                        }
                        System.out.println("编号为"+bianhao+"发送的数据与上次相同，跳过数据表的操作");
                        redisCache.setCacheObject(PREFIX_HEART_DATA_NUM+bianhao,bianhao,2,TimeUnit.HOURS);
                    }
                }
            }
        }
        super.channelRead(ctx, msg);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String channelId =  ctx.channel().id().asShortText();
        redisCache.deleteObject(PREFIX_ID_NUM+channelId);
        super.channelInactive(ctx);
    }

    // 连接成功获取连接数
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }


    //当dtu发送数据时
    private void getData(String number, String jinzhi) {
        //收到数据，设备状态改为正常
        newUpdateLined(number, jinzhi);
        updateLinedState(number, jinzhi);
    }

    //东大数据更新
    public void updateDongda(String number, Map<String, String> allData) {
        if (number != null) {
            Ddlined ddlined = ddlinedMapper.selectByDtuNum(number);
            boolean isWarn = false;
            if (ddlined != null) {
                //提升泵是否运行
                String tsb = allData.get("tsb");
                ddlined.setTsb(tsb);
                //提升泵运行状态
                String tsbzt = allData.get("tsbzt");
                ddlined.setTsbstate(tsbzt);
                if ("1".equals(tsbzt)) {
                    //提升泵报警
                    alterUtil.addDcongdaAlter("提升泵", "提升泵报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("提升泵", "提升泵报警", ddlined, false);
                }
                //自吸泵是否与行
                String zxb = allData.get("zxb");
                ddlined.setZxb(zxb);
                //自吸泵运行状态
                String zxbzt = allData.get("zxbzt");
                ddlined.setZxbstate(zxbzt);
                if ("1".equals(zxbzt)) {
                    //自吸泵过载
                    alterUtil.addDcongdaAlter("自吸泵", "自吸泵报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("自吸泵", "自吸泵报警", ddlined, false);
                }
                //反冲洗泵是否运行
                String fcb = allData.get("fcb");
                ddlined.setFcxb(fcb);
                //反冲洗泵运行状态
                String fcbzt = allData.get("fcbzt");
                ddlined.setFcxbstate(fcbzt);
                if ("1".equals(fcbzt)) {
                    //反冲洗泵报警
                    alterUtil.addDcongdaAlter("反冲洗泵", "反冲洗泵报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("反冲洗泵", "反冲洗泵报警", ddlined, false);
                }
                //鼓风机是否运行
                String gfj = allData.get("gfj");
                ddlined.setGfj(gfj);
                //鼓风机运行状态
                String gfjzt = allData.get("gfjzt");
                ddlined.setGfjstate(gfjzt);
                if ("1".equals(gfjzt)) {
                    //鼓风机报警
                    alterUtil.addDcongdaAlter("鼓风机", "鼓风机报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("鼓风机", "鼓风机报警", ddlined, false);
                }
                //回流泵是否运行
                String hlb = allData.get("hlb");
                ddlined.setHlb(hlb);
                //回流泵运行状态
                String hlbzt = allData.get("hlbzt");
                if ("1".equals(hlbzt)) {
                    //回流泵报警
                    alterUtil.addDcongdaAlter("回流泵", "回流泵报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("回流泵", "回流泵报警", ddlined, false);
                }
                //高压液位机是否运行
                String gywfj = allData.get("gywfj");
                ddlined.setGywfj(gywfj);
                //高压液位机运行状态
                String gywfjzt = allData.get("gywfjzt");
                ddlined.setGywfjstate(gywfjzt);
                if ("1".equals(gywfjzt)) {
                    //高压液位机报警
                    alterUtil.addDcongdaAlter("高液位风机", "高液位风机报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("高液位风机", "高液位风机报警", ddlined, false);
                }
                //低液位风机是否运行
                String dywfj = allData.get("dywfj");
                ddlined.setDywfj(dywfj);
                //低液位风机运行状态
                String dywfjzt = allData.get("dywfjzt");
                ddlined.setDywfj(dywfjzt);
                if ("1".equals(dywfjzt)) {
                    //低液位风机报警
                    alterUtil.addDcongdaAlter("低液位风机", "低液位风机报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("低液位风机", "低液位风机报警", ddlined, false);
                }
                //反洗排水阀
                String fxpsf = allData.get("fxpsf");
                ddlined.setFxpsf(fxpsf);
                //气提泵是否运行
                String qtb = allData.get("qtb");
                ddlined.setQtb(qtb);
                //气提泵运行状态
                String qtbzt = allData.get("qtbzt");
                ddlined.setQtbsatet(qtbzt);
                if ("1".equals(qtbzt)) {
                    //气提泵报警
                    alterUtil.addDcongdaAlter("气提泵", "气提泵报警", ddlined, true);
                    isWarn = true;
                } else {
                    alterUtil.addDcongdaAlter("气提泵", "气提泵报警", ddlined, false);
                }
                if(isWarn){
                   ddlined.setState("1");
                }else {
                    ddlined.setState("0");
                }
                ddlinedMapper.updateByPrimaryKey(ddlined);
                LinedState linedState = linedStateMapper.getLinedStateByField("number", number.trim());
                if (linedState == null) {
                    LinedState linedState1 = new LinedState();
                    linedState1.setTid(UUID.randomUUID().toString());
                    linedState1.setBinding("0");
                    linedState1.setCreatetime(new Date());
                    linedState1.setNumber(number);
                    linedState1.setState("0");
                    linedStateMapper.insertSelective(linedState1);
                }

            }
        }
    }

    private void newUpdateLined(String numberBig, String jinzhi) {
        if (numberBig != null && !"".equals(numberBig)) {
            Lined one = linedMapper.getByDtu(numberBig);
            int num = 0;
            boolean isWarn = false;
            if (one != null) {
                Date date = new Date();
                String chanshuibengState = hexToDecOfString(jinzhi.substring(12, 14) + jinzhi.substring(15, 17));
                String fanxibengState = hexToDecOfString(jinzhi.substring(18, 20) + jinzhi.substring(21, 23));
                String jiaobanjiState = hexToDecOfString(jinzhi.substring(24, 26) + jinzhi.substring(27, 29));
                String gufengji1State = hexToDecOfString(jinzhi.substring(30, 32) + jinzhi.substring(33, 35));
                String gufengji2State = hexToDecOfString(jinzhi.substring(36, 38) + jinzhi.substring(39, 41));
                String jinshuibengState = hexToDecOfString(jinzhi.substring(42, 44) + jinzhi.substring(45, 47));
                String huiliubengState = hexToDecOfString(jinzhi.substring(48, 50) + jinzhi.substring(51, 53));
                int chanshuibengRuntime = hexToDec(jinzhi.substring(54, 56) + jinzhi.substring(57, 59));
                int chanshuibengStoptime = hexToDec(jinzhi.substring(60, 62) + jinzhi.substring(63, 65));
                int fanxibengRuntime = hexToDec(jinzhi.substring(66, 68) + jinzhi.substring(69, 71));
                int fanxibengStoptime = hexToDec(jinzhi.substring(72, 74) + jinzhi.substring(75, 77));
                int gufengjiRuntime = hexToDec(jinzhi.substring(78, 80) + jinzhi.substring(81, 83));
                int gufengjiStoptime = hexToDec(jinzhi.substring(84, 86) + jinzhi.substring(87, 89));
                int jiaobanRuntime = hexToDec(jinzhi.substring(90, 92) + jinzhi.substring(93, 95));
                int jiaobanStoptime = hexToDec(jinzhi.substring(96, 98) + jinzhi.substring(99, 101));
                int huiliubengRuntime = hexToDec(jinzhi.substring(102, 104) + jinzhi.substring(105, 107));
                int huiliubengStoptime = hexToDec(jinzhi.substring(108, 110) + jinzhi.substring(111, 113));
                if (null != one.getChanshuibeng()) {
                    if (!one.getChanshuibeng().equals(chanshuibengState)) {
                        one.setChanshuibengchangetime(date);
                    }
                    //判断过载情况
                    if ("2".equals(chanshuibengState)) {
                        // 产水泵过载
                        alterUtil.addAlter("产水泵", "产水泵过载报警", one, true);
                        isWarn = true;
                    } else {
                        alterUtil.addAlter("产水泵", "产水泵过载报警", one, false);
                    }

                    if (!one.getJinshuibeng().equals(jinshuibengState)) {
                        one.setJinshuibengchangetime(date);
                    }
                    if ("2".equals(fanxibengState)) {
                        alterUtil.addAlter("反洗泵", "反洗泵过载报警", one, true);
                        isWarn = true;
                    } else {
                        alterUtil.addAlter("反洗泵", "反洗泵过载报警", one, false);
                    }

                    if (!one.getGufengji1().equals(gufengji1State)) {
                        one.setGufengji1changetime(date);
                    }
                    //判断过载情况
                    if ("2".equals(gufengji1State)) {
                        alterUtil.addAlter("鼓风机", "鼓风机过载报警", one, true);
                        isWarn = true;
                    } else {
                        alterUtil.addAlter("鼓风机", "鼓风机过载报警", one, false);
                    }

                    if (!one.getJiaobanji().equals(jiaobanjiState)) {
                        one.setJiaobanjichangetime(date);
                    }
                    //判断过载情况
                    if ("2".equals(chanshuibengState)) {
                        alterUtil.addAlter("搅拌机", "搅拌机过载报警", one, true);
                        isWarn = true;
                    } else {
                        alterUtil.addAlter("搅拌机", "搅拌机过载报警", one, false);
                    }

                    if (!one.getHuiliubeng().equals(huiliubengState)) {
                        one.setHuiliubengchangetime(date);
                    }
                    //判断过载情况
                    if ("2".equals(chanshuibengState)) {
                        alterUtil.addAlter("回流泵", "回流泵过载报警", one, true);
                        isWarn = true;
                    } else {
                        alterUtil.addAlter("回流泵", "回流泵过载报警", one, false);
                    }
                }
                if(isWarn){
                    one.setState("1");
                }else {
                    one.setState("0");
                }
                one.setChanshuibeng(chanshuibengState);
                one.setFanxibeng(fanxibengState);
                one.setJiaobanji(jiaobanjiState);
                one.setGufengji1(gufengji1State);
                one.setGufengji2(gufengji2State);
                one.setJinshuibeng(jinshuibengState);
                one.setHuiliubeng(huiliubengState);
                one.setChanshuibengstrattime(chanshuibengRuntime);
                one.setChanshuibengendtime(chanshuibengStoptime);
                one.setFanxibengstrattime(fanxibengRuntime);
                one.setFanxibengendtime(fanxibengStoptime);
                one.setGufengjistrattime(gufengjiRuntime);
                one.setGufengjiendtime(gufengjiStoptime);
                one.setJiaobanjistrattime(jiaobanRuntime);
                one.setJiaobanjiendtime(jiaobanStoptime);
                one.setHuiliubengstarttime(huiliubengRuntime);
                one.setHuiliubengendtime(huiliubengStoptime);
                one.setCreateTime(date);
                linedMapper.updateByPrimaryKey(one);
                DtuRecord dtuRecord = new DtuRecord();
                dtuRecord.setTid(UUID.randomUUID().toString());
                dtuRecord.setSendTime(new Date());
                dtuRecord.setNumber(numberBig);
                dtuRecord.setCun(one.getCun());
                dtuRecord.setCsb(chanshuibengState);
                dtuRecord.setCsbrun(String.valueOf(chanshuibengRuntime));
                dtuRecord.setCsbend(String.valueOf(chanshuibengStoptime));
                dtuRecord.setFxb(fanxibengState);
                dtuRecord.setFxbrun(String.valueOf(fanxibengRuntime));
                dtuRecord.setFxbend(String.valueOf(fanxibengStoptime));
                dtuRecord.setGfj(gufengji1State);
                dtuRecord.setGfjrun(String.valueOf(gufengjiRuntime));
                dtuRecord.setGfjend(String.valueOf(gufengjiStoptime));
                dtuRecord.setHlb(huiliubengState);
                dtuRecord.setHlbrun(String.valueOf(huiliubengRuntime));
                dtuRecord.setHlbend(String.valueOf(huiliubengStoptime));
                dtuRecord.setJbj(jiaobanjiState);
                dtuRecord.setJbjrun(String.valueOf(jiaobanRuntime));
                dtuRecord.setJbjend(String.valueOf(jiaobanStoptime));
                dtuRecordMapper.insertRecord(dtuRecord);
            }
        }
    }


    public void updateAlterValue(String number, String jinzhi) {
        AlertValue alertValue = alertValueMapper.selectByNumber(number);
        Boolean insert = false;
        int chanshuibengRuntime = hexToDec(jinzhi.substring(54, 56) + jinzhi.substring(57, 59));
        int chanshuibengStoptime = hexToDec(jinzhi.substring(60, 62) + jinzhi.substring(63, 65));
        int fanxibengRuntime = hexToDec(jinzhi.substring(66, 68) + jinzhi.substring(69, 71));
        int fanxibengStoptime = hexToDec(jinzhi.substring(72, 74) + jinzhi.substring(75, 77));
        int gufengjiRuntime = hexToDec(jinzhi.substring(78, 80) + jinzhi.substring(81, 83));
        int gufengjiStoptime = hexToDec(jinzhi.substring(84, 86) + jinzhi.substring(87, 89));
        int jiaobanRuntime = hexToDec(jinzhi.substring(90, 92) + jinzhi.substring(93, 95));
        int jiaobanStoptime = hexToDec(jinzhi.substring(96, 98) + jinzhi.substring(99, 101));
        int huiliubengRuntime = hexToDec(jinzhi.substring(102, 104) + jinzhi.substring(105, 107));
        int huiliubengStoptime = hexToDec(jinzhi.substring(108, 110) + jinzhi.substring(111, 113));
        if (alertValue == null) {
            AlertValue alertValue1 = new AlertValue();
            alertValue1.setTid(UUID.randomUUID().toString());
            alertValue1.setNumber(number);
            alertValue1.setCsbStart(chanshuibengRuntime);
            alertValue1.setCsbEnd(chanshuibengStoptime);
            alertValue1.setFxbStart(fanxibengRuntime);
            alertValue1.setFxbEnd(fanxibengStoptime);
            alertValue1.setGfjStart(gufengjiRuntime);
            alertValue1.setGfjEnd(gufengjiStoptime);
            alertValue1.setJbjStart(jiaobanRuntime);
            alertValue1.setJbjEnd(jiaobanStoptime);
            alertValue1.setHlbStart(huiliubengRuntime);
            alertValue1.setHlbEnd(huiliubengStoptime);
            Lined lined = linedMapper.getByDtu(number);
            if (lined != null) {
                alertValue1.setCun(lined.getCun());
                alertValue1.setZhen(lined.getZhen());
            }
            alertValue = alertValue1;
            alertValueMapper.insert(alertValue);
            return;
        }
        if (alertValue.getCsbStart() != chanshuibengRuntime) {
            insert = true;
            alertValue.setCsbStart(chanshuibengRuntime);
        }
        if (alertValue.getCsbEnd() != chanshuibengStoptime) {
            insert = true;
            alertValue.setCsbEnd(chanshuibengStoptime);
        }
        if (alertValue.getFxbStart() != fanxibengRuntime) {
            insert = true;
            alertValue.setFxbStart(fanxibengRuntime);
        }
        if (alertValue.getFxbEnd() != fanxibengStoptime) {
            insert = true;
            alertValue.setFxbEnd(fanxibengStoptime);
        }
        if (alertValue.getGfjStart() != gufengjiRuntime) {
            insert = true;
            alertValue.setGfjStart(gufengjiRuntime);
        }
        if (alertValue.getGfjEnd() != gufengjiStoptime) {
            insert = true;
            alertValue.setGfjEnd(gufengjiStoptime);
        }
        if (alertValue.getJbjStart() != jiaobanRuntime) {
            insert = true;
            alertValue.setJbjStart(jiaobanRuntime);
        }
        if (alertValue.getJbjEnd() != jiaobanStoptime) {
            insert = true;
            alertValue.setJbjEnd(jiaobanStoptime);
        }
        if (alertValue.getHlbStart() != jiaobanRuntime) {
            insert = true;
            alertValue.setJbjStart(jiaobanRuntime);
        }
        if (alertValue.getHlbEnd() != huiliubengStoptime) {
            insert = true;
            alertValue.setHlbEnd(huiliubengStoptime);
        }
        if (insert) {
            if (alertValue.getCun() == null) {
                Lined lined = linedMapper.getByDtu(number);
                if (lined != null) {
                    alertValue.setZhen(lined.getZhen());
                    alertValue.setCun(lined.getCun());
                }
            }
            alertValueMapper.updateByPrimaryKeySelective(alertValue);
        }
    }

    public void updateLinedState(String numberBig, String jinzhi) {

        LinedState one = null;
        Date date = new Timestamp(System.currentTimeMillis());
        LinedState old = linedStateMapper.getLinedStateByField("number", numberBig.trim());
        if (old == null) {
            LinedState linedState = new LinedState();
            linedState.setTid(UUID.randomUUID().toString());
            linedState.setNumber(numberBig);
            linedState.setCreatetime(date);
            linedState.setUpdatetime(date);
            linedState.setState("0");
            linedState.setBinding("0");
            linedStateMapper.insertSelective(linedState);
            one = linedState;
        } else {
            one = old;
        }
        if (numberBig != null && !"".equals(numberBig)) {
            // one = linedStateMapper.getLinedStateByField("number", numberBig);
            if (one != null) {
                String chanshuibengState = hexToDecOfString(jinzhi.substring(12, 14) + jinzhi.substring(15, 17));
                String fanxibengState = hexToDecOfString(jinzhi.substring(18, 20) + jinzhi.substring(21, 23));
                String jiaobanjiState = hexToDecOfString(jinzhi.substring(24, 26) + jinzhi.substring(27, 29));
                String gufengji1State = hexToDecOfString(jinzhi.substring(30, 32) + jinzhi.substring(33, 35));
                String gufengji2State = hexToDecOfString(jinzhi.substring(36, 38) + jinzhi.substring(39, 41));
                String jinshuibengState = hexToDecOfString(jinzhi.substring(42, 44) + jinzhi.substring(45, 47));
                String huiliubengState = hexToDecOfString(jinzhi.substring(48, 50) + jinzhi.substring(51, 53));
                int chanshuibengRuntime = hexToDec(jinzhi.substring(54, 56) + jinzhi.substring(57, 59));
                int chanshuibengStoptime = hexToDec(jinzhi.substring(60, 62) + jinzhi.substring(63, 65));
                int fanxibengRuntime = hexToDec(jinzhi.substring(66, 68) + jinzhi.substring(69, 71));
                int fanxibengStoptime = hexToDec(jinzhi.substring(72, 74) + jinzhi.substring(75, 77));
                int gufengjiRuntime = hexToDec(jinzhi.substring(78, 80) + jinzhi.substring(81, 83));
                int gufengjiStoptime = hexToDec(jinzhi.substring(84, 86) + jinzhi.substring(87, 89));
                int jiaobanRuntime = hexToDec(jinzhi.substring(90, 92) + jinzhi.substring(93, 95));
                int jiaobanStoptime = hexToDec(jinzhi.substring(96, 98) + jinzhi.substring(99, 101));
                int huiliubengRuntime = hexToDec(jinzhi.substring(102, 104) + jinzhi.substring(105, 107));
                int huiliubengStoptime = hexToDec(jinzhi.substring(108, 110) + jinzhi.substring(111, 113));
                if (!chanshuibengState.equals(one.getChanshuibeng())) {
                    one.setChanshuibengchangetime(date);
                }
                if (!(fanxibengState).equals(one.getFanxibeng())) {
                    one.setFanxibengchangetime(date);
                }
                if (!(jiaobanjiState).equals(one.getJiaobanji())) {
                    one.setJiaobanjichangetime(date);
                }
                if (!(gufengji1State).equals(one.getGufengji1())) {
                    one.setGufengji1changetime(date);
                }
                if (!(gufengji2State).equals(one.getGufengji2())) {
                    one.setGufengji2changetime(date);
                }
                if (!(jiaobanjiState).equals(one.getJinshuibeng())) {
                    one.setJiaobanjichangetime(date);
                }
                if (!(huiliubengState).equals(one.getHuiliubeng())) {
                    one.setHuiliubengchangetime(date);
                }
                one.setChanshuibeng(chanshuibengState);
                one.setFanxibeng(fanxibengState);
                one.setJiaobanji(jiaobanjiState);
                one.setGufengji1(gufengji1State);
                one.setGufengji2(gufengji2State);
                one.setJinshuibeng(jinshuibengState);
                one.setHuiliubeng(huiliubengState);
                one.setChanshuibengstrattime(chanshuibengRuntime);
                one.setChanshuibengendtime(chanshuibengStoptime);
                one.setFanxibengstrattime(fanxibengRuntime);
                one.setFanxibengendtime(fanxibengStoptime);
                one.setGufengjistrattime(gufengjiRuntime);
                one.setGufengjiendtime(gufengjiStoptime);
                one.setJiaobanjistrattime(jiaobanRuntime);
                one.setJiaobanjiendtime(jiaobanStoptime);
                one.setHuiliubengstarttime(huiliubengRuntime);
                one.setHuiliubengendtime(huiliubengStoptime);
                one.setState("0");
                one.setUpdatetime(date);
                linedStateMapper.updateByPrimaryKeySelective(one);
            }
        }
    }


    //十六进制装十进制
    public static String hexToDecOfString(String hex) {                //转化为十六进制
        int decValue = 0;
        for (int i = 0; i < hex.length(); i++) {                   //字符串的位置从0位开始
            char hexChar = hex.charAt(i);                           //获取十六进制各个位置的内容
            decValue = decValue * 16 + hexCharToDec(hexChar);     //计算转化为十进制的值
        }
        return String.valueOf(decValue);
    }

    public static int hexCharToDec(char ch) {             //将A~F的转化为相应的数值10~15
        if (ch >= 'A' && ch <= 'F')
            return 10 + ch - 'A';
        else
            return ch - '0';
    }

    public static String bytesToHexString(byte[] src) {
        StringBuffer sb = new StringBuffer("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                sb.append(0);
            }
            sb.append(hv);
            if (i != src.length - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    //十六进制转十进制
    public static int hexToDec(String hex) {                //转化为十六进制
        int decValue = 0;
        for (int i = 0; i < hex.length(); i++) {                   //字符串的位置从0位开始
            char hexChar = hex.charAt(i);                           //获取十六进制各个位置的内容
            decValue = decValue * 16 + hexCharToDec(hexChar);     //计算转化为十进制的值
        }
        return decValue;
    }

    public static Integer getFen(Date satrtTime, Date endTime) {
        if (satrtTime == null) {
            return 0;
        }
        long cha = endTime.getTime() - satrtTime.getTime();
        return (int) cha / (1000 * 60);
    }

    //获取东大设备所有状态
    public Map<String, String> getAllState(String data) {
        String[] s = data.split(" ");
        Map<String, String> map = new HashMap<>();
        map.put("tsb", jieQu(s[0]));
        map.put("tsbzt", jieQu(s[1]));
        map.put("zxb", jieQu(s[2]));
        map.put("zxbzt", jieQu(s[3]));
        map.put("fcb", jieQu(s[4]));
        map.put("fcbzt", jieQu(s[5]));
        map.put("gfj", jieQu(s[6]));
        map.put("gfjzt", jieQu(s[7]));
        map.put("hlb", jieQu(s[8]));
        map.put("hlbzt", jieQu(s[9]));
        map.put("gywfj", jieQu(s[10]));
        map.put("gywfjzt", jieQu(s[11]));
        map.put("dywfj", jieQu(s[12]));
        map.put("dywfjzt", jieQu(s[13]));
        map.put("fxpsf", jieQu(s[14]));
        map.put("qtb", jieQu(s[15]));
        map.put("qtbzt", jieQu(s[16]));
        return map;
    }

    //状态去除前一位"0"
    public String jieQu(String str) {
        return str.substring(1);
    }

    //断开链接排查
    public  void pcDklj(String num) {
        //先查看redis是否有断开链接记录
        String redisValue = redisCache.getCacheObject(PREFIX_DKLJ_NUM + num);
        if (redisValue != null) {
            //有记录
            if (num.contains("WZSNWV1")) {
                //钟海龙
                Lined lined = linedMapper.getByDtu(num);
                if (lined != null) {
                    lined.setState("3");
                    linedMapper.updateByPrimaryKey(lined);
                    redisCache.deleteObject(PREFIX_DKLJ_NUM + num);
                    redisCache.setCacheObject(PREFIX_NBYC_NUM + num, num);
                    System.out.println("dtu编号为" + num + "断开链接已经重连！");
                }
            } else {
                //东大
                Ddlined ddlined = ddlinedMapper.selectByDtuNum(num);
                if (ddlined != null) {
                    ddlined.setState("3");
                    ddlinedMapper.updateByPrimaryKey(ddlined);
                    redisCache.deleteObject(PREFIX_DKLJ_NUM + num);
                    redisCache.setCacheObject(PREFIX_NBYC_NUM + num, num);
                    System.out.println("dtu编号为" + num + "断开链接已经重连！");
                }
            }
        }
    }

    //内部异常排查
    public  void pcNbjc(String num){
        //先查看redis是否有内部异常记录
        String redisValue = redisCache.getCacheObject(PREFIX_NBYC_NUM + num);
        if(redisValue!= null){
          if(num.contains("WZSNWV1")){
              Lined lined = linedMapper.getByDtu(num);
              if(lined!=null){
                  lined.setState("0");
                  linedMapper.updateByPrimaryKey(lined);
                 redisCache.deleteObject(PREFIX_NBYC_NUM+num);
                  System.out.println("dtu编号为" + num + "内部异常已经重连！");
              }
          } else {
             Ddlined ddlined = ddlinedMapper.selectByDtuNum(num);
             if(ddlined!= null){
               ddlined.setState("0");
               ddlinedMapper.updateByPrimaryKey(ddlined);
               redisCache.deleteObject(PREFIX_NBYC_NUM+num);
                 System.out.println("dtu编号为" + num + "内部异常已经重连！");
             }
          }
        }
    }




}
